//
//  RETProductDescriptionCell.m
//  Retail
//
//  Created by ChenJie on 4/1/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "RETProductDescriptionCell.h"
#import "OABase.h"

typedef NS_ENUM(NSUInteger, ComponentId) {
    TopSeparator = 1,
    ContentLabel,
    BottomSeparator,
};

@interface RETProductDescriptionCell ()

//@property (nonatomic, strong) UIView *topSeparator;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIView *bottomSeparator;

@end

@implementation RETProductDescriptionCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
//        [self.contentView addSubview:self.topSeparator];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.bottomSeparator];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.contentLabel.frame = [self.viewModel.oa_layouts[@(ContentLabel)] CGRectValue];
    self.bottomSeparator.frame = [self.viewModel.oa_layouts[@(BottomSeparator)] CGRectValue];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods

#define kContentFont [UIFont systemFontOfSize:20]
#define kMarginTop 40
#define kMarginBottom 40
#define kSeparatorHeight 1

+ (CGFloat)oa_heightWithWidth:(CGFloat)cellWidth viewModel:(RETProductDescriptionCellViewModel *)viewModel {
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    CGFloat topMark = 0;
    
    topMark += kMarginTop;
    CGSize contentSize = [viewModel.text oa_sizeWithWidth:cellWidth font:kContentFont];
    CGRect contentFrame = CGRectMake(0, topMark, contentSize.width, contentSize.height);
    layouts[@(ContentLabel)] = OAValueWithRect(contentFrame);
    topMark += contentSize.height;
    
    topMark += kMarginBottom;
    CGRect bottomSeparatorFrame = CGRectMake(0, topMark, cellWidth, kSeparatorHeight);
    layouts[@(BottomSeparator)] = OAValueWithRect(bottomSeparatorFrame);
    
    viewModel.oa_layouts = layouts;
    
    return CGRectGetMaxY(bottomSeparatorFrame);
}

#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(RETProductDescriptionCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.contentLabel.text = viewModel.text;
}

#pragma mark - Getter

OAGetView(bottomSeparator, ^(UIView *view){
    view.backgroundColor = OAColor(dddddd);
})

OAGetLabel(contentLabel, ^(UILabel *label){
    label.font = kContentFont;
    label.textColor = OAColor(333333);
    label.numberOfLines = 0;
})

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
